
package com.oahcfly.chgame.core.ui;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.Animation.PlayMode;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.oahcfly.chgame.core.mvc.CHActor;

/**
 * 
 * gif播放器
 * @author haocao
 *
 */
public class CHGifActor extends CHActor {

	private CHGifDecoder gifDecoder;

	private Animation animation;

	public CHGifActor () {
		gifDecoder = new CHGifDecoder();
	}

	public void loadGif (String path) {
		gifDecoder.read(Gdx.files.internal(path).read());
		animation = gifDecoder.getAnimation(PlayMode.LOOP);
		TextureRegion region = animation.getKeyFrame(0);
		setWidth(region.getRegionWidth());
		setHeight(region.getRegionHeight());
	}

	float stateTime = 0l;

	@Override
	public void draw (Batch batch, float parentAlpha) {
		// TODO Auto-generated method stub
		super.draw(batch, parentAlpha);

		TextureRegion region = animation.getKeyFrame(stateTime);
		batch.draw(region, getX(), getY());
		if (playing) stateTime += Gdx.graphics.getDeltaTime();
	}

	private boolean playing = true;

	public void play () {
		playing = true;
	}

	public void pause () {
		playing = false;
	}

	public CHGifDecoder getGifDecoder () {
		return gifDecoder;
	}

	public Animation getAnimation () {
		return animation;
	}
	
	
}
